﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlxRRSDB;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB.Data;
using System.Data.SqlClient;
using System.Data;
using GlxRRSDB.Utils;

namespace GlxRRSDB.Facades
{
    public class WeeklyReportFacade
    {
        private static Dictionary<int, int> dictTotalSeat = null;
        private static SqlConnection _connection = null;

        private static SqlConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    _connection = new SqlConnection();
                    _connection.ConnectionString = GlxRRSDB.Properties.Settings.Default.ReportDBConnectionString;
                }
                return _connection;
            }
        }

        private const string SQL_DETAIL_FILM_ADMISSION = "SELECT FilmID, CinemaID, Admission, Date, FilmTitle, StartDate, FinishDate " +
                                   "FROM FilmFilmRevenueView " +
                                   "WHERE (DATEDIFF(day, (@fromdate), DATE) >= 0) AND (DATEDIFF(day, (@todate), DATE) <= 0)" +
                                   "ORDER BY FilmID ASC, DATE ASC";
        private const string SQL_DETAIL_FILM_BO = "SELECT FilmID, CinemaID, BO, Date, FilmTitle " +
                                   "FROM FilmFilmRevenueView " +
                                   "WHERE (DATEDIFF(day, (@fromdate), DATE) >= 0) AND (DATEDIFF(day, (@todate), DATE) <= 0)" +
                                   "ORDER BY FilmID ASC, DATE ASC";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentDate"></param>
        /// <param name="dict">film id, cinema id, data</param>
        /// <returns></returns>
        public static Dictionary<int, Dictionary<int, WeeklyReportData>> getDetailFilmAdmissionWeek(DateTime currentDate, Dictionary<int, Dictionary<int, WeeklyReportData>> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(SQL_DETAIL_FILM_ADMISSION);
            cmd.Connection = Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = currentDate.StartOfWeek(DayOfWeek.Friday);
            cmd.Parameters[1].Value = currentDate.EndOfWeek(DayOfWeek.Friday);
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int filmId = reader.GetInt32(0);
                    int cinemaId = reader.GetInt32(1);
                    int admission = reader.GetInt32(2);
                    DateTime date = reader.GetDateTime(3);
                    string filmTitle = reader.GetString(4);
                    DateTime startDate = reader.GetDateTime(5);
                    DateTime finishDate = reader.GetDateTime(6);
                    //
                    if (dict.ContainsKey(filmId))
                    {
                        Dictionary<int, WeeklyReportData> dictDateTime = dict[filmId];
                        if (dictDateTime.ContainsKey(cinemaId))
                        {
                            WeeklyReportData data = dictDateTime[cinemaId];
                            data.SumWeekAdmission += admission;
                        }
                        else
                        {
                            WeeklyReportData data = new WeeklyReportData();
                            data.CinemaID = cinemaId;
                            data.FilmTitle = filmTitle;
                            data.CurrentDate = currentDate;
                            data.SumWeekAdmission = admission;
                            data.StartDate = startDate;
                            data.FinishDate = finishDate;
                            //
                            dictDateTime.Add(cinemaId, data);
                        }
                    }
                    else
                    {
                        Dictionary<int, WeeklyReportData> dictDateTime = new Dictionary<int, WeeklyReportData>();
                        WeeklyReportData data = new WeeklyReportData();
                        data.CinemaID = cinemaId;
                        data.FilmTitle = filmTitle;
                        data.CurrentDate = currentDate;
                        data.SumWeekAdmission = admission;
                        data.StartDate = startDate;
                        data.FinishDate = finishDate;
                        //
                        dictDateTime.Add(cinemaId, data);
                        //
                        dict.Add(filmId, dictDateTime);
                    }
                }
            }
            reader.Close();

            return dict;
        }
        public static Dictionary<int, Dictionary<int, WeeklyReportData>> getDetailFilmBOWeek(DateTime fromdate, Dictionary<int, Dictionary<int, WeeklyReportData>> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(SQL_DETAIL_FILM_BO);
            cmd.Connection = Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = fromdate.StartOfWeek(DayOfWeek.Friday);
            cmd.Parameters[1].Value = fromdate.EndOfWeek(DayOfWeek.Friday);
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int filmId = reader.GetInt32(0);
                    int cinemaId = reader.GetInt32(1);
                    long bo = reader.GetInt64(2);
                    DateTime date = reader.GetDateTime(3);
                    string filmTitle = reader.GetString(4);
                    //
                    if (dict.ContainsKey(filmId))
                    {
                        Dictionary<int, WeeklyReportData> dictDateTime = dict[filmId];
                        if (dictDateTime.ContainsKey(cinemaId))
                        {
                            WeeklyReportData data = dictDateTime[cinemaId];
                            data.SumWeekBO += bo;
                        }
                        else
                        {
                            WeeklyReportData data = new WeeklyReportData();
                            data.CinemaID = cinemaId;
                            data.FilmTitle = filmTitle;
                            data.CurrentDate = fromdate;
                            data.SumWeekBO = bo;
                            dictDateTime.Add(cinemaId, data);
                        }
                    }
                    else
                    {
                        Dictionary<int, WeeklyReportData> dictDateTime = new Dictionary<int, WeeklyReportData>();
                        WeeklyReportData data = new WeeklyReportData();
                        data.CinemaID = cinemaId;
                        data.FilmTitle = filmTitle;
                        data.CurrentDate = fromdate;
                        data.SumWeekBO = bo;
                        dictDateTime.Add(cinemaId, data);
                        //
                        dict.Add(filmId, dictDateTime);
                    }
                }
            }
            reader.Close();

            return dict;
        }
    }
}
